package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.StockTaskDetailAdapter;
import com.mflex.WHSmartWhouseAd.model.ActionPdaInventoryTaskDto;
import com.mflex.WHSmartWhouseAd.model.DoTransTaskInfo;
import com.mflex.WHSmartWhouseAd.model.RequestModel;
import com.mflex.WHSmartWhouseAd.model.WHINVTaskResult;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.mflex.WHSmartWhouseAd.model.WHCompCheckTaskDetails;

import com.mflex.WHSmartWhouseAd.model.WHMBarcodeInfor;
import com.mflex.WHSmartWhouseAd.utils.HideKeyBoardUtil;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
//import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class StockTaskListDetailActivity extends BaseActivity {

    EditText stvTaskNo;

    EditText stvOperator;

    EditText stvWHLocName;

    EditText stvBoxId;

    EditText stvPKG;
    ListView lvTaskList;
    EditText stvUnScanVehQty;
    EditText stvScanVehQty;

    SuperButton buttonSave;

    SuperButton buttonClear;

    MiniLoadingDialog mMiniLoadingDialog;

    private String userName;
    private String taskNo;
    private String taskID;

    private String Type;

    private int ScanQty = 0;
    private int UnScanQty = 0;
    private List<WHCompCheckTaskDetails> whCompCheckTaskDetailsList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_task_list_detail);

        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载中...");

        stvTaskNo = findViewById(R.id.stv_TaskNo);
        stvOperator = findViewById(R.id.stv_Operator);
        stvWHLocName = findViewById(R.id.stv_WHLocName);
        stvBoxId = findViewById(R.id.stv_BoxId);
        lvTaskList = findViewById(R.id.lvTaskList);
        buttonSave = findViewById(R.id.button_save);
        buttonClear = findViewById(R.id.button_clear);

        stvUnScanVehQty = findViewById(R.id.stvUnScanVehQty);
        stvScanVehQty = findViewById(R.id.stvScanVehQty);

        stvPKG = findViewById(R.id.stvPKG);

        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        taskNo = intent.getStringExtra("taskNo");

        taskID = intent.getStringExtra("ID");

        Type = intent.getStringExtra("Type");

        stvWHLocName.setEnabled(false);
        if (StringUtils.equals("非智能仓", Type)) {
            stvWHLocName.setEnabled(true);
        }

        stvOperator.setOnKeyListener(this);
        stvWHLocName.setOnKeyListener(this);
        stvBoxId.setOnKeyListener(this);
        buttonSave.setOnClickListener(this);
        buttonClear.setOnClickListener(this);
        stvOperator.requestFocus();

        stvOperator.setFocusable(true);
        stvOperator.setFocusableInTouchMode(true);
        stvOperator.requestFocus();
        HideKeyBoardUtil.disableShowSoftInput(stvOperator);
        stvOperator.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    HideKeyBoardUtil.hideKeyBoard(StockTaskListDetailActivity.this);
                }
            }
        });
        initData();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_clear:
                clearButtonMd(view);
                break;
            case R.id.button_save:
                back();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_Operator:
//                    chekOperator();
                    break;
                case R.id.stv_WHLocName:
                    checkWHLocName();
                    break;
                case R.id.stv_BoxId:
                    checkBoxNumber(view);
                    stvBoxId.requestFocus();
                    break;
                case R.id.stvPKG:
                    checkPKGNumber(view);
                    break;
                default:
                    break;
            }


        }
        return false;
    }

    private void RefData() {
        mMiniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/PDAGetInventoryTaskDetail_info")
                .params("TaskId", taskID.trim())
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<List<WHCompCheckTaskDetails>>() {

                    @Override
                    public void onSuccess(List<WHCompCheckTaskDetails> response) {
                        mMiniLoadingDialog.dismiss();
                        whCompCheckTaskDetailsList = response;
                        stvTaskNo.setText(taskNo);
                        ScanQty = 0;
                        UnScanQty = 0;
                        for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
                            if (StringUtils.equals("2", details.getStatus())) {
                                ScanQty = ScanQty + 1;
                            } else {
                                UnScanQty = UnScanQty + 1;
                            }
                        }


                        Collections.sort(whCompCheckTaskDetailsList, new Comparator<WHCompCheckTaskDetails>() {
                            @Override
                            public int compare(WHCompCheckTaskDetails t0, WHCompCheckTaskDetails t1) {
                                if (!StringUtils.equals("2", t0.getStatus()) && StringUtils.equals("2", t1.getStatus())) {
                                    return -1;
                                }
                                return 0;

                            }
                        });
                        StockTaskDetailAdapter stockTaskDetailAdapter = new StockTaskDetailAdapter(StockTaskListDetailActivity.this, whCompCheckTaskDetailsList);
                        lvTaskList.setAdapter(stockTaskDetailAdapter);
                        stvUnScanVehQty.setText(String.valueOf(UnScanQty));
                        stvScanVehQty.setText(String.valueOf(ScanQty));
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
                        mp.start();
                        new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }

    private void LocData() {
        mMiniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/PDAGetInventoryTaskDetail_info")
                .params("TaskId", taskID.trim())
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<List<WHCompCheckTaskDetails>>() {

                    @Override
                    public void onSuccess(List<WHCompCheckTaskDetails> response) {
                        mMiniLoadingDialog.dismiss();
                        whCompCheckTaskDetailsList = response;
                        stvTaskNo.setText(taskNo);
                        ScanQty = 0;
                        UnScanQty = 0;


                        Collections.sort(whCompCheckTaskDetailsList, new Comparator<WHCompCheckTaskDetails>() {
                            @Override
                            public int compare(WHCompCheckTaskDetails t0, WHCompCheckTaskDetails t1) {
                                if (!StringUtils.equals("2", t0.getStatus()) && StringUtils.equals("2", t1.getStatus())) {
                                    return -1;
                                }
                                return 0;

                            }
                        });
                        StockTaskDetailAdapter stockTaskDetailAdapter = new StockTaskDetailAdapter(StockTaskListDetailActivity.this, whCompCheckTaskDetailsList);
                        lvTaskList.setAdapter(stockTaskDetailAdapter);
                        for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
                            if (StringUtils.equals("2", details.getStatus())) {
                                ScanQty = ScanQty + 1;
                            } else {
                                UnScanQty = UnScanQty + 1;
                            }
                        }
                        stvUnScanVehQty.setText(String.valueOf(UnScanQty));
                        stvScanVehQty.setText(String.valueOf(ScanQty));
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
                        mp.start();
                        new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }

    private void initData() {
        mMiniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/PDAGetInventoryTaskDetail_info")
                .params("TaskId", taskID.trim())
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<List<WHCompCheckTaskDetails>>() {

                    @Override
                    public void onSuccess(List<WHCompCheckTaskDetails> response) {
                        mMiniLoadingDialog.dismiss();
                        whCompCheckTaskDetailsList = response;
                        stvTaskNo.setText(taskNo);

                        for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
                            if (StringUtils.equals("2", details.getStatus())) {
                                ScanQty = ScanQty + 1;
                            } else {
                                UnScanQty = UnScanQty + 1;
                            }
                        }


                        Collections.sort(whCompCheckTaskDetailsList, new Comparator<WHCompCheckTaskDetails>() {
                            @Override
                            public int compare(WHCompCheckTaskDetails t0, WHCompCheckTaskDetails t1) {
                                if (!StringUtils.equals("2", t0.getStatus()) && StringUtils.equals("2", t1.getStatus())) {
                                    return -1;
                                }
                                return 0;

                            }
                        });
                        StockTaskDetailAdapter stockTaskDetailAdapter = new StockTaskDetailAdapter(StockTaskListDetailActivity.this, whCompCheckTaskDetailsList);
                        lvTaskList.setAdapter(stockTaskDetailAdapter);
                        stvUnScanVehQty.setText(String.valueOf(UnScanQty));
                        stvScanVehQty.setText(String.valueOf(ScanQty));
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
                        mp.start();
                        new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }

    private void checkPKGNumber(View view) {
        if (StringUtils.isEmpty(stvOperator.getText().toString())) {
            MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
            mp.start();
            new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("操作人未输入")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }
        if (StringUtils.isEmpty(stvPKG.getText().toString())) {
            MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
            mp.start();
            new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("库位未输入")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }
        if (StringUtils.isEmpty(stvBoxId.getText().toString())) {
            MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
            mp.start();
            new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("载具/箱未输入")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }

        for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
            if (StringUtils.equals(stvBoxId.getText().toString(), details.getWHName()) && StringUtils.equals("2", details.getStatus())) {
                MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
                mp.start();
                new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                        .iconRes(R.drawable.icon_warning)
                        .title(R.string.tip_warning)
                        .content("箱已盘点")
                        .positiveText(R.string.lab_submit)
                        .show();
                return;
            }
        }
        boolean flag = false;
        for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
            if (StringUtils.equals(stvBoxId.getText().toString(), details.getWHName())) {
                flag = true;
            }
        }
        if (flag) {
            MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
            mp.start();
            new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("箱不待盘列表中")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }

        if (StringUtils.isEmpty(stvOperator.getText().toString())) {
            return;
        }
        mMiniLoadingDialog.show();


        ActionPdaInventoryTaskDto model = new ActionPdaInventoryTaskDto(taskID, stvWHLocName.getText().toString(), "", stvPKG.getText().toString(), stvOperator.getText().toString());
        checkPKGName(model, view);
//        stvPKG.requestFocus();
        stvPKG.setText("");

    }

    private void checkBoxNumber(View view) {
        if (StringUtils.isEmpty(stvOperator.getText().toString())) {
            MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
            mp.start();
            new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("操作人未输入")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }

        if (StringUtils.equals("非智能仓", Type)) {
            if (StringUtils.isEmpty(stvWHLocName.getText().toString())) {
                MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
                mp.start();
                new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                        .iconRes(R.drawable.icon_warning)
                        .title(R.string.tip_warning)
                        .content("库位未输入")
                        .positiveText(R.string.lab_submit)
                        .show();
                return;
            }
        }

        if (StringUtils.isEmpty(stvBoxId.getText().toString())) {
            MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
            mp.start();
            new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("载具/箱未输入")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }

        for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
            if (StringUtils.equals(stvBoxId.getText().toString(), details.getWHName()) && StringUtils.equals("2", details.getStatus())) {
                MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
                mp.start();
                new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                        .iconRes(R.drawable.icon_warning)
                        .title(R.string.tip_warning)
                        .content("箱已盘点")
                        .positiveText(R.string.lab_submit)
                        .show();
                return;
            }
        }
        boolean flag = false;
        for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
            if (StringUtils.equals(stvBoxId.getText().toString(), details.getWHName())) {
                flag = true;
            }
        }
        if (flag) {
            MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
            mp.start();
            new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("箱不待盘列表中")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }

        if (StringUtils.isEmpty(stvOperator.getText().toString())) {
            return;
        }
        mMiniLoadingDialog.show();


        ActionPdaInventoryTaskDto model = new ActionPdaInventoryTaskDto(taskID, stvWHLocName.getText().toString(), stvBoxId.getText().toString(), "", stvOperator.getText().toString());
        checkName(model, view);

        stvBoxId.setText("");
        stvBoxId.requestFocus();
    }

    private void checkName(ActionPdaInventoryTaskDto model, View view) {
        XHttp.post("api/WHSmartWhousePDA/ActionPdaInventoryTask")
                .upJson(JsonUtil.toJson(model))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(150000)
                .execute(new SimpleCallBack<WHINVTaskResult>() {

                    @Override
                    public void onSuccess(WHINVTaskResult response) {
                        mMiniLoadingDialog.dismiss();
                        UnScanQty = 0;
                        ScanQty = 0;
                        //20250327添加钢片原材料跳转页面
                        boolean flg = response.getIsPAC();
                        if (flg) {
                            Intent intent = new Intent(StockTaskListDetailActivity.this, StockTaskListDetaillistActivity.class);
                            intent.putExtra("Reelid", response.getREELID().toString().trim());
                            intent.putExtra("MPN", response.getMPN().toString().trim());
                            intent.putExtra("TotalQty", response.getConCount().toString().trim());
                            intent.putExtra("Qty", response.getHaveCheckQty().toString().trim());
                            intent.putExtra("userName", stvOperator.getText().toString());
                            intent.putExtra("TaskID", taskID);
                            intent.putExtra("list", JsonUtil.toJson(response.getWaitCheckSonList()));
                            startActivityForResult(intent,10001);
                        } else {
                            for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
                                if (StringUtils.equals(response.getDetailId(), details.getDetailID())) {
                                    details.setStatus("2");
                                }
                            }
                            Collections.sort(whCompCheckTaskDetailsList, new Comparator<WHCompCheckTaskDetails>() {
                                @Override
                                public int compare(WHCompCheckTaskDetails t0, WHCompCheckTaskDetails t1) {
                                    if (!StringUtils.equals("2", t0.getStatus()) && StringUtils.equals("2", t1.getStatus())) {
                                        return -1;
                                    }
                                    return 0;

                                }
                            });
                            int CheckStatus = 0;
                            for (WHCompCheckTaskDetails model :
                                    whCompCheckTaskDetailsList) {
                                if (!StringUtils.equals(model.getStatus(), "2")) {
                                    CheckStatus = 1;
                                    UnScanQty = UnScanQty + 1;
                                } else {
                                    ScanQty = ScanQty + 1;
                                }
                            }
                            if (CheckStatus == 0) {
                                clearButtonMd(view);
                            } else {
                                StockTaskDetailAdapter stockTaskDetailAdapter = new StockTaskDetailAdapter(StockTaskListDetailActivity.this, whCompCheckTaskDetailsList);
                                lvTaskList.setAdapter(stockTaskDetailAdapter);
                            }
                            stvUnScanVehQty.setText(String.valueOf(UnScanQty));
                            stvScanVehQty.setText(String.valueOf(ScanQty));
                            stvBoxId.setText("");
                            stvBoxId.requestFocus();
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvBoxId.setText("");
                        stvBoxId.requestFocus();
                        MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
                        mp.start();
                        new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }

    private void checkPKGName(ActionPdaInventoryTaskDto model, View view) {
        XHttp.post("api/WHSmartWhousePDA/ActionPdaInventoryTask")
                .upJson(JsonUtil.toJson(model))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(150000)
                .execute(new SimpleCallBack<WHINVTaskResult>() {

                    @Override
                    public void onSuccess(WHINVTaskResult response) {
                        mMiniLoadingDialog.dismiss();
                        UnScanQty = 0;
                        ScanQty = 0;

                        //20250327添加钢片原材料跳转页面
                        boolean flg = response.getIsPAC();
                        if (flg) {
                            Intent intent = new Intent(StockTaskListDetailActivity.this, StockTaskListDetaillistActivity.class);
                            intent.putExtra("Reelid", response.getREELID().toString().trim());
                            intent.putExtra("MPN", response.getMPN().toString().trim());
                            intent.putExtra("UNQty", response.getConCount().toString().trim());
                            intent.putExtra("Qty", response.getHaveCheckQty().toString().trim());
                            intent.putExtra("userName", stvOperator.getText().toString());
                            intent.putExtra("TaskID", taskID);
                            intent.putExtra("list", JsonUtil.toJson(response.getWaitCheckSonList()));
                            startActivityForResult(intent,10001);
                        } else {
                            for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
                                if (StringUtils.equals(response.getDetailId(), details.getDetailID())) {
                                    details.setStatus("2");
                                }
                            }
                            Collections.sort(whCompCheckTaskDetailsList, new Comparator<WHCompCheckTaskDetails>() {
                                @Override
                                public int compare(WHCompCheckTaskDetails t0, WHCompCheckTaskDetails t1) {
                                    if (!StringUtils.equals("2", t0.getStatus()) && StringUtils.equals("2", t1.getStatus())) {
                                        return -1;
                                    }
                                    return 0;

                                }
                            });
                            int CheckStatus = 0;
                            for (WHCompCheckTaskDetails model :
                                    whCompCheckTaskDetailsList) {
                                if (!StringUtils.equals(model.getStatus(), "2")) {
                                    CheckStatus = 1;
                                    UnScanQty = UnScanQty + 1;
                                } else {
                                    ScanQty = ScanQty + 1;
                                }
                            }
                            if (CheckStatus == 0) {
                                clearButtonMd(view);
                            } else {
                                StockTaskDetailAdapter stockTaskDetailAdapter = new StockTaskDetailAdapter(StockTaskListDetailActivity.this, whCompCheckTaskDetailsList);
                                lvTaskList.setAdapter(stockTaskDetailAdapter);
                            }
                            stvUnScanVehQty.setText(String.valueOf(UnScanQty));
                            stvScanVehQty.setText(String.valueOf(ScanQty));
                            stvPKG.requestFocus();
                            stvPKG.setText("");
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvPKG.requestFocus();
                        stvPKG.setText("");
                        MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
                        mp.start();
                        new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }

    private void checkWHLocName() {

        if (StringUtils.equals("非智能仓", Type)) {
            boolean flag = false;
            for (WHCompCheckTaskDetails details : whCompCheckTaskDetailsList) {
                if (StringUtils.equals(details.getWHName(), stvWHLocName.getText().toString())) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                MediaPlayer mp = MediaPlayer.create(StockTaskListDetailActivity.this, R.raw.error_voice);
                mp.start();
                new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                        .iconRes(R.drawable.icon_warning)
                        .title(R.string.tip_warning)
                        .content("库位:" + stvWHLocName.getText().toString()+"和实际盘点库位不一致")
                        .positiveText(R.string.lab_submit)
                        .show();
                stvWHLocName.setText("");
                stvWHLocName.requestFocus();
                return;
            }
            LocData();
        }

    }

    private void clearButtonMd(View view) {
        stvBoxId.setText("");
        stvWHLocName.setText("");
        stvWHLocName.requestFocus();
        RefData();
    }

    private void back() {
        setResult(0);
        finish();
    }

    private void chekOperator() {
        if (StringUtils.isEmpty(stvOperator.getText().toString())) {
            return;
        }
        mMiniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/CheckOperator")
                .params("OptFuncName", "元件盘点")
                .params("Operator", stvOperator.getText().toString())
                .syncRequest(false)
                .onMainThread(true)
                .execute(new SimpleCallBack<Boolean>() {
                    @Override
                    public void onSuccess(Boolean response) {
                        mMiniLoadingDialog.dismiss();
                        stvWHLocName.requestFocus();
                    }

                    @Override
                    public void onError(ApiException e) {
                        stvOperator.setText("");
                        stvOperator.requestFocus();
                        mMiniLoadingDialog.dismiss();
                        new MaterialDialog.Builder(StockTaskListDetailActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initData();
    }
}